<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <img src="../assets/dao.jpg" alt="头像" />
      </div>

      <!-- 表单区域 -->
      <el-form ref="LoginFormRef" class="login_form" :model="LoginForm" :rules="LoginFormRules">
        <el-tag type="danger" v-if="flag">用户名帐号或密码错误，请检查您的帐号与密码</el-tag>
        <!-- 帐号 -->
        <el-form-item prop="username">
          <el-input placeholder="帐号" prefix-icon="el-icon-user-solid" v-model="LoginForm.username"></el-input>
        </el-form-item>

        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            placeholder="密码"
            prefix-icon="el-icon-lock"
            v-model="LoginForm.password"
            type="password"
          ></el-input>
        </el-form-item>

        <!-- 按钮区域 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      flag: false,
      // 数据绑定
      LoginForm: {
        username: "admin",
        password: "123456"
      },
      // 表单验证
      LoginFormRules: {
        // 帐号验证
        username: [
          { required: true, message: "请输入帐号名称", trigger: "blur" },
          { min: 2, max: 10, message: "长度在 2 到 10 个字符", trigger: "blur" }
        ],
        // 密码验证
        password: [
          { required: true, message: "请输入帐号密码", trigger: "blur" },
          { min: 6, max: 15, message: "长度在 6 到 15 个字符", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    //重置按钮功能
    reset() {
      this.$refs.LoginFormRef.resetFields();
      this.flag = false;
    },
    login() {
      this.$refs.LoginFormRef.validate(async valid => {
        if (!valid) {
          return;
        }
        const { data: result } = await this.$http.post("login", this.LoginForm);
        if (result.meta.status != 200) {
          this.flag = true;
        }
        window.sessionStorage.setItem("token", result.data.token);
        this.$router.push("/home");
      });
    }
  }
};
</script>

<style lang="less" scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
  background-image: url("../assets/1.jpg");
  background-size: 100%;
}
.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  box-shadow: 0 0 5px 5px #ccc;

  .avatar_box {
    height: 130px;
    width: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    background-color: #fff;
    box-shadow: 0 0 10px #eee;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);

    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }

  .btns {
    display: flex;
    justify-content: space-evenly;
  }
}

.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}

.login_box::before,.login_box::after{
  content: '';
  border: 2px solid #2ecc71;
  position: absolute;
  z-index: -1;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  margin: -15px;
  animation: borderClip 8s cubic-bezier(.17,1.08,1,1.39) infinite;
}
.login_box::after{
  animation-delay: -4s;
}
@keyframes borderClip {
  0%,100%{
    clip: rect(0,478px,2px,0);
  }
  25%{
    clip: rect(0,2px,328px,0);
  }
  50%{
    clip: rect(328px,478px,330px,0);
  }
  75%{
    clip: rect(0,480px,328px,478px);
  }
}

</style>